home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6975 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  92 lines

  1. Newsgroups: comp.lang.c++
  2. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!cronkite!news
  3. From: Marco DeFreitas <mdefreitas@sikorsky.com>
  4. Subject: Re: Virtuals in constructor
  5. Content-Type: text/plain; charset=us-ascii
  6. Message-ID: <1996Feb21.011424.21712@cronkite.res.utc.com>
  7. Sender: news@cronkite.res.utc.com
  8. Nntp-Posting-Host: iris604.asi.sikorsky.com
  9. Content-Transfer-Encoding: 7bit
  10. Organization: Sikorsky Aircraft
  11. References: <312A3A72.688D@scopus.ch>
  12. Mime-Version: 1.0
  13. Date: Wed, 21 Feb 1996 01:14:24 GMT
  14. X-Mailer: Mozilla 1.1S (X11; I; IRIX 5.3 IP12)
  15. X-Url: news:312A3A72.688D@scopus.ch
  16.  
  17. Laurent,
  18.  
  19. It looks as though your pure virtual function should be called from the
  20. class that instanced it.  BTW, it typically is not safe to call a derived 
  21. class fuction from the base class constructor, since the derived class
  22. function may used derived members (which haven't been created yet, since
  23. you are still in the base class).  Try the following, hope it helps:
  24.  
  25.  
  26. class A
  27. {
  28.   public:
  29.     A(VOID) {}
  30.  
  31.     virtual VOID Method(VOID) = 0;
  32. };
  33.  
  34. class B : public A
  35. {
  36.   public:
  37.     B(VOID) {Method();}
  38.  
  39.     VOID Method(VOID) { MessageBox(NULL, "", "", MB_OK); }
  40. };
  41.  
  42. int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  43. {
  44.     B b;
  45.  
  46.     return 0;
  47. }
  48.  
  49.  
  50. -------
  51. Regards,
  52. Sikorsky Aircraft
  53. Stratford, CT
  54.  
  55. >
  56. Hi,
  57. >
  58. >does anyone know why the following code doesn't work ?
  59. >
  60. >Run-time error: pure virtual function called
  61. >
  62. >class A
  63. >{
  64. >  public:
  65. >    A(VOID) { Method(); }
  66. >
  67. >    virtual VOID Method(VOID) = 0;
  68. >};
  69. >
  70. >class B : public A
  71. >{
  72. >  public:
  73. >    B(VOID) {}
  74. >
  75. >    VOID Method(VOID) { MessageBox(NULL, "", "", MB_OK); }
  76. >};
  77. >
  78. >int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  79. >{
  80. >    B b;
  81. >
  82. >    return 0;
  83. >}
  84. >
  85. >Thanks
  86. >
  87. >                                Laurent Guinnard
  88. >                                guinnard@eig.unige.ch
  89. >
  90. >be_well++;
  91.  
  92.